package org.helsoft.mojo.api;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Version implements Comparable<Version>{

	private int major = 1;
	private int minor;
	private int build = 0;
	private String qualifier = "";
	
	public Version(){		
	}
	
	public Version(String str) {
		Pattern pattern = Pattern.compile("(\\d+)\\.(\\d+)(\\.(\\d+))?");
		Matcher matcher = pattern.matcher(str.trim());
		if (matcher.matches()) {
			this.major = Integer.valueOf(matcher.group(1));
			this.minor = Integer.valueOf(matcher.group(2));
			if (matcher.groupCount() == 3) {
				this.build = Integer.valueOf(matcher.group(3));
			}
		}
	}
	
	public Version(int major, int minor, int build, String qualifier) {
		this.major = major;
		this.minor = minor;
		this.build = build;
		this.qualifier = qualifier;
	}
	
	public Version(int major, int minor, int build) {
		this(major, minor, build, "");
	}
	
	/**
	 * @return the major
	 */
	public final int getMajor() {
		return major;
	}

	/**
	 * @param major the major to set
	 */
	public final void setMajor(int major) {
		this.major = major;
	}

	/**
	 * @return the minor
	 */
	public final int getMinor() {
		return minor;
	}

	/**
	 * @param minor the minor to set
	 */
	public final void setMinor(int minor) {
		this.minor = minor;
	}

	/**
	 * @return the build
	 */
	public final int getBuild() {
		return build;
	}

	/**
	 * @param build the build to set
	 */
	public final void setBuild(int build) {
		this.build = build;
	}

	public String getQualifier() {
		return qualifier;
	}

	public void setQualifier(String qualifier) {
		this.qualifier = qualifier;
	}

	@Override
	public int compareTo(Version o) {
		if (major < o.major) {
			return -1;
		} else if (major > o.major) {
			return 1;
		} else if (minor < o.minor) {
			return -1;
		} else if (major > o.major) {
			return 1;
		} else if (build < o.build) {
			return -1;
		} else if (build > o.build) {
			return 1;
		}
		return 0;
	}
	
	@Override
	public String toString() {
		return major + "." + minor + (build > - 1 ? ("." + build) : "") + (qualifier == null || qualifier.isEmpty() ? "" : ("-" + qualifier));
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + build;
		result = prime * result + major;
		result = prime * result + minor;
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Version other = (Version) obj;
		if (build != other.build)
			return false;
		if (major != other.major)
			return false;
		if (minor != other.minor)
			return false;
		return true;
	}

	public static Version fromFileName(String name) {
		String regex = "[^_]+_(\\d+)\\.(\\d+)\\.(\\d{1,5})?.*";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(name);
		if (matcher.matches()) {
			int major = Integer.valueOf(matcher.group(1));
			int minor = Integer.valueOf(matcher.group(2));
			int build = matcher.groupCount() == 3 ? Integer.valueOf(matcher.group(3)) : -1;
			return new Version(major, minor, build);
		}
		return null;
	}

	
}
